package com.malamute.system.action.system;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;

import com.malamute.core.action.BaseAction;
import com.malamute.core.action.CrudMethod;
import com.malamute.core.action.PrepareHelper;
import com.malamute.core.common.SearchParamUtil;
import com.malamute.system.entity.Role;
import com.malamute.system.service.RoleService;

/**
 * @author Aceguan
 * @description TODO
 * @date 2012-2-27 下午10:43:04
 * @version v1.0
 */
public class RoleAction extends BaseAction implements CrudMethod, PrepareHelper {
	private Role role;
	@Autowired
	private RoleService roleService;

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	@Override
	public void prepare() throws Exception {
		setActionName(getActionName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.PrepareHelper#prepareInput()
	 */
	@Override
	public void prepareInput() throws Exception {
		// link树林

	}

	public String form() throws Exception {
		if (role != null) {
			setRole(roleService.getRoleById(role.getId()));
		}
		return FORM;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#save()
	 */
	@Override
	public String save() throws Exception {
		roleService.saveRole(role);
		return ACTION;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#update()
	 */
	@Override
	public String update() throws Exception {
		roleService.updateRole(role);
		return ACTION;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#list()
	 */
	@Override
	public String list() throws Exception {
		DetachedCriteria criteria = DetachedCriteria.forClass(Role.class);
		SearchParamUtil.simpleSeach(criteria, searchParam);
		roleService.listRolesByPage(criteria, page);
		return SUCCESS;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#search()
	 */
	@Override
	public String search() throws Exception {
		return this.list();
	}

}
